package com.tadpole.music.presenter.study;

import com.tadpole.music.bean.study.QuestionCountHeadBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.study.QuestionCountIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 答题统计上部
 * Created By 孙岩
 * on 2020/9/29
 */
public class QuestionCountPresenter extends BasePresenter<QuestionCountIView> {
    @SuppressWarnings("unchecked")
    public void getQuestionCountHead( final String college_name, final String class_name, String college_id, String rank_id, String class_id, String q_type) {
        if (isViewAttached()) {
            String url = UrlConfig.questionCount + "?college_id=" + college_id + "&rank_id=" + rank_id + "&class_id=" + class_id + "&q_type=" + q_type;
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
            DataModel.request(ModelToken.GET_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if (obj.getInt("code") == 200) {
                                    JSONObject object = new JSONObject(obj.getString("data"));
                                    QuestionCountHeadBean.DataBean bean = new QuestionCountHeadBean.DataBean(
                                            JsonParseUtil.getStr(object, "rate"),
                                            JsonParseUtil.getStr(object, "score"),
                                            JsonParseUtil.getStr(object, "time")
                                    );
                                    getView().showQuestionCount(bean,college_name,class_name);

                                } else if (obj.getInt("code") == 401) {
                                    getView().show401();
                                } else {
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }

    @SuppressWarnings("unchecked")
    public void getQuestionCountHeadStudent(final String college_name, String college_id,String rank_id, String q_type,String student_id) {
        if (isViewAttached()) {
            String url = UrlConfig.questionCount + "?college_id=" + college_id +"&rank_id="+rank_id+ "&q_type=" + q_type +"&user_id="+student_id;
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
            DataModel.request(ModelToken.GET_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if (obj.getInt("code") == 200) {
                                    JSONObject object = new JSONObject(obj.getString("data"));
                                    QuestionCountHeadBean.DataBean bean = new QuestionCountHeadBean.DataBean(
                                            JsonParseUtil.getStr(object, "rate"),
                                            JsonParseUtil.getStr(object, "score"),
                                            JsonParseUtil.getStr(object, "time")
                                    );
                                    getView().showQuestionCount(bean,college_name,"");

                                } else if (obj.getInt("code") == 401) {
                                    getView().show401();
                                } else {
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }
}
